#!/bin/bash
set -e

if [ $# -eq 2 -a "x${2}" = "xsubbuild" ]; then
	echo "配置环境变量: [已配置]"
else
	echo "配置环境变量: [开始]"
	#>>-----------------------------------------------------------------------------------
	#切换目录
	cd ../
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#顶层项目目录
	export PROJECT_TOPDIR=$PWD
	#顶层当前配置文件
	export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
	#-------------------------------------------------------------------------------------

	#>>env--------------------------------------------------------------------------------
	#设置颜色函数
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh > /dev/null
	#导入公共变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh > /dev/null
	#导入平台变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh > /dev/null
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#切回目录
	cd -> /dev/null
	#-------------------------------------------------------------------------------------
	echo "配置环境变量: [完毕]"
fi

#-----------------------------------------------------------------------
#导入公共函数
source 000.import.commonfunc.sh > /dev/null
#-----------------------------------------------------------------------

function total_build_module()
{
	pinfo "批量构建... [开始]"
	for file in $(ls *.build_bctest*.sh)
	do
		pinfo "执行文件: $file [开始]"
		. $file build subbuild
		if [ $? -eq 0 ]; then
			pdone "执行文件: $file [完毕]"
		else
			perro "执行文件: $file [失败]"
		fi
	done
	pdone "批量构建... [结束]"
}
function total_clean_module()
{
	pinfo "批量清除... [开始]"
	for file in $(ls *.build_bctest*.sh)
	do
		pinfo "执行文件: $file [开始]"
		. $file clean subbuild
		if [ $? -eq 0 ]; then
			pdone "执行文件: $file [完毕]"
		else
			perro "执行文件: $file [失败]"
		fi
	done
	pdone "批量清除... [结束]"
}
function total_pack_module()
{
	pinfo "批量打包... [开始]"
	for file in $(ls *.build_bctest*.sh)
	do
		pinfo "执行文件: $file [开始]"
		. $file pack subbuild
		if [ $? -eq 0 ]; then
			pdone "执行文件: $file [完毕]"
		else
			perro "执行文件: $file [失败]"
		fi
	done
	pdone "批量打包... [结束]"
}

function total_install_module()
{
	#编译时安装目录
	MODOUT_TOPDIR=${APPKIT_TOPDIR}/out

	#最终文件系统安装目录
	MODINS_TOPDIR=${PROJECT_TOPDIR}/z.src.rtfs/usr/local/bin

	if [ ! -d ${MODINS_TOPDIR} ]; then
		pwarn "创建跟文件系统安装目录... [开始]"
		pinfo "sudo mkdir -p ${MODINS_TOPDIR}"
		sudo mkdir -p ${MODINS_TOPDIR}
		sdone "创建跟文件系统安装目录... [完毕]"
	fi

	pinfo "批量安装... [开始]"
	for subdir in $(ls ${MODOUT_TOPDIR} )
	do
		pinfo "拷贝: $subdir [开始]"
		echo "sudo cp -rpf ${MODOUT_TOPDIR}/${subdir}/* ${MODINS_TOPDIR}/"
		sudo cp -rpf ${MODOUT_TOPDIR}/${subdir}/* ${MODINS_TOPDIR}/
		if [ $? -eq 0 ]; then
			pdone "拷贝: $subdir [完毕]"
		else
			perro "拷贝: $subdir [失败]"
		fi
	done
	pdone "批量安装... [完毕]"
}

function total_mainproc() 
{
	if [ "x$1" = "xbuild" ]; then
		total_build_module
	elif [ "x$1" = "xclean" ]; then
    	total_clean_module
	elif [ "x$1" = "xpack" ]; then
		total_pack_module
	elif [ "x$1" = "xinstall" ]; then
		pinfo "安装appkit文件... [开始]"
		total_install_module
		pdone "安装appkit文件... [完毕]"
	else
		sinfo "Usage:"
		sinfo " $0 build "
		sinfo " $0 clean "
		sinfo " $0 pack"
		sinfo " $0 install"
	fi
}
total_mainproc $1
